package org.apache.qpid.management.common.sasl;

import java.io.IOException;
import org.apache.harmony.javax.security.auth.callback.Callback;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.javax.security.auth.callback.NameCallback;
import org.apache.harmony.javax.security.auth.callback.PasswordCallback;
import org.apache.harmony.javax.security.auth.callback.UnsupportedCallbackException;

public class UserPasswordCallbackHandler
  implements CallbackHandler
{
  private char[] pwchars;
  private String user;

  public UserPasswordCallbackHandler(String paramString1, String paramString2)
  {
    this.user = paramString1;
    this.pwchars = paramString2.toCharArray();
  }

  private void clearPassword()
  {
    int i;
    if (this.pwchars != null)
      i = 0;
    while (true)
    {
      if (i >= this.pwchars.length)
      {
        this.pwchars = null;
        return;
      }
      this.pwchars[i] = ';
      ++i;
    }
  }

  protected void finalize()
  {
    clearPassword();
  }

  public void handle(Callback[] paramArrayOfCallback)
    throws IOException, UnsupportedCallbackException
  {
    int i = 0;
    if (i >= paramArrayOfCallback.length)
      return;
    if (paramArrayOfCallback[i] instanceof NameCallback)
      ((NameCallback)paramArrayOfCallback[i]).setName(this.user);
    while (true)
    {
      while (true)
        ++i;
      if (!(paramArrayOfCallback[i] instanceof PasswordCallback))
        break;
      ((PasswordCallback)paramArrayOfCallback[i]).setPassword(this.pwchars);
    }
    throw new UnsupportedCallbackException(paramArrayOfCallback[i]);
  }
}